package com.yanghx.order.server.controller;

import com.yanghx.order.server.message.StreamClient;
import com.yanghx.order.server.vo.dto.OrderDTO;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * rabbitmq 消息测试
 *
 * @author yangHX
 * createTime  2019/9/13 22:14
 */
@RestController
@RequestMapping
public class SendMessageController {
    @Resource
    private AmqpTemplate amqpTemplate;
    @Resource
    private StreamClient streamClient;

    @GetMapping("/sendMessage")
    public void process() {
        String message = "now " + new Date();
        amqpTemplate.convertAndSend("myQueue", message);
    }


    /**
     * 通过路由发消息
     */
    @GetMapping("/message/router")
    public void processByRouter(String router) {
        String RouterKey_computer = "computer";
        String RouterKey_fruit = "fruit";

        String RouterKey = null;
        if (RouterKey_computer.equals(router)) {
            RouterKey = RouterKey_computer;
        }
        if (RouterKey_fruit.equals(router)) {
            RouterKey = RouterKey_fruit;
        }
        amqpTemplate.convertAndSend("myOrder", RouterKey, "now " + new Date());
    }


    @RequestMapping(value = "/message/stream")
    public void processStream() {
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setOrderId("123456");
        streamClient.input().send(MessageBuilder.withPayload(orderDTO).build());

    }


}
